package com.glory.brepda.activity.check;


import android.annotation.SuppressLint;
import android.content.Intent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.brepda.R;
import com.glory.brepda.adapter.QCCheckAdapter;
import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.base.Constant;
import com.glory.brepda.entity.QCCheckBean;
import com.glory.brepda.entity.response.BaseResponseBean;
import com.glory.brepda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_PopUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * QC检验
 */
public class QCCheckActivity extends BaseActivity {

    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.etWorkID)
    FreeUI_ClearEditText etWorkID;

    @BindView(R.id.quipmentID)
    FreeUI_ClearEditText quipmentID;

    @BindView(R.id.lotId)
    FreeUI_ClearEditText lotId;
    @BindView(R.id.rvCheckInfoList)
    RecyclerView rvCheckInfoList;

    private QCCheckAdapter mAdapter;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_qccheck;
    }

    @SuppressLint("SetTextI18n")
    @Override
    protected void initView() {

        setMainLayout(llMain);
        tvTitle.setText("QC检验");
        tvBaseSubmit.setText("查询");
        tvBaseSubmit.setVisibility(View.VISIBLE);
        initRecyclerView(rvCheckInfoList,this);
        mAdapter = new QCCheckAdapter(null,QCCheckActivity.this);
        mAdapter.isFirstOnly(true);
        mAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        rvCheckInfoList.setAdapter(mAdapter);


    }

    @Override
    protected void initData() {

        /*initEdit(etWorkID,s->{
            queryCheck();
        });*/

        tvBaseSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(checkIsNull(etWorkID.getText())&&checkIsNull(quipmentID.getText())&&checkIsNull(lotId.getText())   ){
                    showToast("任务二维码、设备编号、机台编号必输其一", false);
                    return;
                }
                queryCheck();
            }
        });

    }


    public boolean checkIsNull(Object str){
        return str == null || StringUtils.isEmpty(str.toString()) || StringUtils.isBlank(str.toString());
    }

    private void queryCheck(){

        FreeUI_EntityCallBack<BaseResponseBean<QCCheckBean>> callback = new FreeUI_DialogEntityCallBack<BaseResponseBean<QCCheckBean>>
                (new TypeToken<BaseResponseBean<QCCheckBean>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<QCCheckBean>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {

                    mAdapter.setNewData(response.body().getResponse().getBody().getQCFORM());
                    mAdapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<QCCheckBean>> response) {
                super.onError(response);
                loadError(response.getException(), "CPCQCFORMQUERY");
            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("GROUPID", getText(etWorkID));
        map.addProperty("USERNAME", Constant.CURRENT_USER);
        map.addProperty("LOTID", getText(lotId));
        map.addProperty("EQUIPMENTID", getText(quipmentID));
        NetUtils.requestPostNet("IF.CPCQCFORMQUERY", "Common", QCCheckActivity.this, map, callback);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 1 && resultCode == RESULT_OK) {
            queryCheck();
        }
    }
}